package net.zhengxinyun.performance.controller.indicators;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.TownIndicatorsEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;
import net.zhengxinyun.performance.service.indicators.TownScoreService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.report.IndexReportService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/indexReport")
public class IndexReportCtrl {

    @Autowired
    private LoginService loginService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private IndexReportService indexReportService;

    @Autowired
    private TownScoreService townScoreService;



    @RequestMapping("/getReport")
    public Result<HashMap<String,Object>> queryReport(@RequestBody ReportParam param, HttpServletRequest request)
    {
        String
        year= (String) request.getSession().getAttribute("currentYear");
        if(year==null)
        {
            return ResultUtils.getResults(Result.Status.OVERTIME,"登录超时，请重新登录",null);
        }
        param.townIndicatorsFilter.setDateTime(year);
        //indexclass 1 市对县， 2 县对乡镇 3 县直单位

        if(tokenService.compareToken(param.tokenEntity.getValue())){
            UserEntity user = loginService.getUserByToken(param.tokenEntity);
            HashMap<String,Object> maps = new HashMap<>();
          if(2==param.townIndicatorsFilter.getIndexClass())
          {
              List<HashMap<String, Object>> hashMaps = indexReportService.queryReport(param.townIndicatorsFilter);
              maps.put("list", hashMaps);
          }else if(1==param.townIndicatorsFilter.getIndexClass())
          {
              List<HashMap<String, Object>> hashMaps =indexReportService.queryCityReport(param.townIndicatorsFilter);
              maps.put("list", hashMaps);
          }else
              if(3==param.townIndicatorsFilter.getIndexClass())
              {
                  List<HashMap<String, Object>> hashMaps =indexReportService.queryDepartReport(param.townIndicatorsFilter);
                  maps.put("list", hashMaps);
              }


            return  ResultUtils.getSuccessResults(maps);
        }

        return ResultUtils.getResults(Result.Status.OVERTIME,"token匹配错误",null);
    }

}

class ReportParam{
    public TokenEntity tokenEntity;
    public TownIndicatorsFilter townIndicatorsFilter;
    public TownIndicatorsEntity townIndicatorsEntity;
}